Reuse transport and signaling client for dialing with webrtc #674
+144
−56
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The
dialWebRTCfunction created multipleTransportinstances and signaling service clients during connection setup; however, the client retained only the final WebRTC data channel transport, which caused a few issues.HTTP/2 connection pool buildup
createGrpcWebTransportfrom@connectrpc/connect-webcreates HTTP/2 connections. TheTransportusesfetch, and the browser manages persistent connections in its connection pool. By reusing a singleTransportinstance, we enable the browser to reuse the same underlying HTTP/2 connection for multiple requests to the same origin, rather than potentially creating multiple connection contexts through separate 'Transport' objects.Memory leaks from abandoned transport states
Transportinstances hold internal state (connection pools, request queues, event handlers, authentication headers).The transport from
getOptionalWebRTCConfigwas never explicitly closed. TheTransportobject goes out of scope, but its internal state may persist until the garbage collector runs. With frequent reconnections, this accumulates.Port exhaustion on frequent reconnects
We created multiple
Transportobjects for each connection attempt, which could open numerous TCP connections, each consuming a temporary local port. During rapid reconnections, these ports might not be released fast enough, risking port exhaustion. By creating and reusing a singleTransportinstance, the browser can efficiently manage a single underlying connection and its port, eliminating the risk of exhaustion.Multiple connections to the signaling service
We were creating two separate client connections to the signaling server: one for
optionalWebRTCConfigand one for signaling exchange. These changes create a singleTransportwhich means one connection handles both operations, reducing server load and connection overhead.